package com.heo.coder.packet;

import com.heo.coder.packet.base.BasePacket;
import com.heo.util.ByteHelper;
import com.heo.util.Utils;

public class SettingPacketRsp extends BasePacket{

	public SettingPacketRsp(String commandId, String reserve, String subcontract, String rsa, int bodyLength, String imei,
			String sequenceId, String body, String check) throws Exception {
		super(commandId, reserve, subcontract, rsa, bodyLength, imei, sequenceId, body, check);
	}
	
	/**
	 * 
	 * @param apn
	 * @param domain
	 * @param port
	 * @param sleepTime 关锁情况下，多久上传一次位置 单位s
	 * @param workTime  开锁情况下，多久上传一次位置 单位s
	 */
	public void setMsgContent(String apn, String domain, int port, int sleepTime, int workTime) {
		int size = 0;
		StringBuffer contBody = new StringBuffer();
		if(Utils.checkNull(apn)) {
			size++;
			contBody.append("0010");
			String apnDomain = ByteHelper.strTo16(apn);
			contBody.append(ByteHelper.byteToHexStringNoSpace(new byte[] {ByteHelper.intToByte(apn.length()/ 2)}));
			contBody.append(apnDomain);
		}
		
		if(Utils.checkNull(domain)) {
			size++;
			contBody.append("0013");
			String hexDomain = ByteHelper.strTo16(domain);
			contBody.append(ByteHelper.byteToHexStringNoSpace(new byte[] {ByteHelper.intToByte(hexDomain.length()/ 2)}));
			contBody.append(hexDomain);
		}
		
		if(port > 0) {
			size++;
			contBody.append("0018");
			String hexPort = ByteHelper.byteToHexStringNoSpace(ByteHelper.intToByteArray(port));
			byte[] bodyByte = ByteHelper.hexStringToBytes(hexPort);
			int bodyInt = bodyByte.length;
			contBody.append(ByteHelper.byteToHexStringNoSpace(new byte[] {ByteHelper.intToByte(bodyInt)}));
			contBody.append(hexPort);
		}
		
		if(sleepTime > 0) {
			size++;
			contBody.append("0029");
			String hexSleepTime = ByteHelper.byteToHexStringNoSpace(ByteHelper.intToByteArray(sleepTime));
			byte[] bodyByte = ByteHelper.hexStringToBytes(hexSleepTime);
			int bodyInt = bodyByte.length;
			contBody.append(ByteHelper.byteToHexStringNoSpace(new byte[] {ByteHelper.intToByte(bodyInt)}));
			contBody.append(hexSleepTime);
		}
		
		if(workTime > 0) {
			size++;
			contBody.append("0027");
			String hexWorkTime = ByteHelper.byteToHexStringNoSpace(ByteHelper.intToByteArray(workTime));
			byte[] bodyByte = ByteHelper.hexStringToBytes(hexWorkTime);
			int bodyInt = bodyByte.length;
			contBody.append(ByteHelper.byteToHexStringNoSpace(new byte[] {ByteHelper.intToByte(bodyInt)}));
			contBody.append(hexWorkTime);
		}
		
		String hexLength = ByteHelper.byteToHexStringNoSpace(new byte[] {ByteHelper.intToByte(size)});
		StringBuffer rspBody = new StringBuffer();
		rspBody.append(hexLength);
		rspBody.append(contBody.toString());
		
		body = rspBody.toString();
		byte[] bodyByte = ByteHelper.hexStringToBytes(body);
		int bodyInt = bodyByte.length;
		String binary = reserve + subcontract + rsa + ByteHelper.hex10To2String(bodyInt, 10);
		String hex = ByteHelper.binaryString2hexString(binary);
		setHead(hex);
		setBodyLength(bodyInt);
		
		String hexCheck = getCommandId() + head + imei + sequenceId + body;
    	byte[] hexCheckBytes = ByteHelper.hexStringToBytes(hexCheck);
		byte xorByte = ByteHelper.getXor(hexCheckBytes);
		String checkCodeStr = ByteHelper.byteToHexString(new byte[] {xorByte});
		check = checkCodeStr;
	}
	
	@Override
	public String toString() {
		return "SocketPacket: [" + commandId + "] [" + START_MARKER_LENGTH_CODE + getCommandId() + getHead() + getImei() + getSequenceId() + getBody() + getCheck() + END_MARKER_LENGTH_CODE + "]";
	}

	@Override
	public String getPacketBody() {
		return super.body;
	}

	
	
}
